#用于生成一个表格，基于openxlpy实现Excel的读写
#关键信息包括：1、表格头部文本
#           2、单行头部
#           3、用户元素
#           4、元素分隔符
#           5、单行尾部
#           6、表格尾部

import openpyxl
import os
import sys

def ShowString(srt = ""):
    print(srt, end = '')
    return srt


class TTabGen:

    def __init__(self, file_path = '', build_path = '', user_param = None):
        self._build_path = build_path
        self._file_path = file_path
        self._is_open = False
        self._table_title = []
        self.user_param = user_param
        print("file_path --> build_path")

    def _OpenExcel(self):
        self._wb = openpyxl.load_workbook(self._file_path, read_only = True, data_only = True)
        self._config_ws = self._wb["config"]
        self._table_ws = self._wb["table"]
        self._is_open = True
        return True

    def _TStart(self):
        # config相关信息
        self._tab_head_symbol = self._config_ws["B1"].value
        self._row_head_symbol = self._config_ws["B2"].value
        self._space_symbol = self._config_ws["B3"].value
        self._row_tail_symbol = self._config_ws["B4"].value
        self._tab_tail_symbol = self._config_ws["B5"].value
        self._row_count = self._config_ws["B6"].value

        self._row_string_flag = self._config_ws["B8"].value
        self._space_flag = self._config_ws["B9"].value
        self._row_end_flag = self._config_ws["B10"].value
        self._tab_end_flag = self._config_ws["B11"].value

        # 获取表格table表头信息
        col = 1
        temp_run = True
        while (temp_run):
            cell = self._table_ws.cell(1, col).value
            self._table_title.append(cell)
            if cell == self._row_end_flag:
                temp_run = False
            else:
                col += 1
            pass

        return col + 1

    def _GetRowData(self, row:int):
        result = []
        col = 1
        temp_run = True

        while (temp_run):
            flag = self._table_title[col - 1]

            if flag == self._row_end_flag:
                temp_run = False
            else:
                cell = self._table_ws.cell(row, col).value
                result.append(cell)
                col += 1
            pass


        return result


    def TDoWork(self, callback = None):
        self._OpenExcel()
        self._TStart()

        others_data = ''

        fd = open(self._build_path, "w", encoding='utf-8')

        # 写表格头
        fd.write("{0}\n".format(self._tab_head_symbol))

        # 写行
        row = 2
        while (self._table_ws.cell(row, 1).value != self._tab_end_flag):
            fd.write(ShowString("{0}".format(self._row_head_symbol)))
            temp_run = True
            col = 1
            while(temp_run):
                cell = self._table_title[col - 1]
                if cell == self._row_string_flag:
                    fd.write(ShowString("{0}".format(self._table_ws.cell(row, col).value)))
                    col += 1
                elif cell == self._space_flag:
                    fd.write(ShowString("{0} ".format(self._space_symbol)))
                    col += 1
                elif cell == self._row_end_flag:
                    temp_run = False
                else:
                    fd.write(ShowString(" __ERROR__cell({0}, {1}) ".format(row, col)))
                    temp_run = False
                pass
            fd.write(ShowString("{0}\n".format(self._row_tail_symbol)))

            if callable(callback):
                others_data += callback(self._GetRowData(row))
            row += 1

        #写表格尾 + 共计生成行对象数量
        fd.write(ShowString("{0}  <<共计生成行数量: {1} 项>>\n".format(self._tab_tail_symbol, (row - 2))))

        fd.write("\n======================= other callback ==========================\n")
        fd.write(others_data)

        fd.close()
        return

# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':

    def EndCallback(data: list):
        result = ''
        return result

    argc = len(sys.argv)
    argv = sys.argv
    
    if argc == 1:
        tg = TTabGen("tab_gen_mode.xlsx", "tab_gen_mode.txt")
        tg.TDoWork(EndCallback)
    elif argc < 1:
        print("Params Error\n")
    else:
        for f in range(1, argc):
            tg_out = argv[f].replace(".xlsx", ".txt")
            tg = TTabGen(argv[f], tg_out)
            tg.TDoWork(EndCallback)
            del tg
        
    # if callable(ShowString):
    #     print("111")
    # else:
    #     print("222")





























